﻿using System;
using System.Configuration;
using System.Data;
using System.Drawing.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Billing.Controls;
using Billing.Dtos;
using Billing.Utilities;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for CreditNotes.xaml
    /// </summary>
    public partial class CreditNotes : Window
    {
        CreditNoteDto m_creditNoteDto = new CreditNoteDto();
        CreditNoteBillingGrid grid = null;

        public CreditNotes()
        {
            InitializeComponent();
            //stk1.Height = 0;
            stk2.Height = 0;
            cmbCompanyOrganization.DataContext = Database.GetCreditNoteCompanies();
            cmbCompanyOrganization.Focus();
            cmbCompanyOrganization.SelectedIndex = 0;
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else if (e.Key == Key.F1)
                {
                    if (txtAmount.Text.Trim() == string.Empty)
                    {
                        if (grid == null)
                        {
                            grid = new CreditNoteBillingGrid(((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[1].ToString(), Convert.ToInt32(((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[0].ToString()));
                            grid.Owner = this;
                            grid.ShowDialog();
                            //txtBillNo.Text = grid.BillNo;
                            //txtAmount.Text = grid.TotalAmount.ToString();
                            this.Close();
                        }
                        else
                            this.Close();
                        /*BillingGrid billingGrid = new BillingGrid(true);
                        billingGrid.Owner = this;
                        Storage.CreditNoteCompany = ((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[1].ToString();
                        Storage.CreditNoteID = Convert.ToInt32(((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[0].ToString());
                        billingGrid.ShowDialog();
                        m_creditNoteDto.NetTotalAmount = billingGrid.TotalNetAmount;
                        txtAmount.Text = billingGrid.TotalNetAmount.ToString();
                        m_creditNoteDto.CompanyName = ((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[1].ToString();
                        m_creditNoteDto.CompanyID = Convert.ToInt32(((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[0].ToString());
                        txtBillNo.Text = m_creditNoteDto.BillNumber = billingGrid.rBillNo.Text;*/
                    }
                }
                /*else if (e.Key == Key.F2)
                {
                    if (txtBillNo.Text != string.Empty && txtAmount.Text != string.Empty)
                    {
                        if (MessageBoxResult.OK == MessageBox.Show("Do you want to Add new Credit Note?", "Confirm Add Credit Note", MessageBoxButton.OKCancel))
                        {
                            m_creditNoteDto.BillNumber = txtBillNo.Text;
                            m_creditNoteDto.CompanyName = ((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[1].ToString();
                            m_creditNoteDto.CompanyID = Convert.ToInt32(((cmbCompanyOrganization as ListBox).SelectedItem as DataRowView).Row[0].ToString());
                            m_creditNoteDto.NetTotalAmount = decimal.Parse(txtAmount.Text);

                            int result = Database.AddCreditNote(m_creditNoteDto);
                            if (result == -2)
                            {
                                MessageBox.Show("Credit Note for this bill is already added.", "Invalid Bill No", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                            else if (result > 0)
                            {
                                MessageBox.Show("Credit Note for " + m_creditNoteDto.CompanyName.ToString() + " is added", "Credit Note Added", MessageBoxButton.OK, MessageBoxImage.Information);
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("Some Unknown Error has occurred.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                    }
                }*/
            }
            catch (Exception) { }
        }

        private void cmbCompanyOrganization_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Right)
                    txtBillNo.Focus();
            }
            catch (Exception) { }
        }

        private void txtBillNo_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Left)
                {
                    txtBillNo.Clear();
                    cmbCompanyOrganization.Focus();
                }
                else if ((e.Key == Key.Enter || e.Key == Key.F2) && txtBillNo.Text != string.Empty)
                {
                    DataTable ds = Database.GetCreditNoteBill(txtBillNo.Text);
                    if (ds != null && ds.Rows.Count > 0)
                    {
                        lblBillResult.Content = string.Empty;
                        Storage.LoginUser = ds.Rows[0]["Cashier"].ToString();
                        Storage.CreditNoteCompany = ds.Rows[0]["CreditNoteCompany"].ToString();
                        Storage.BillNo = ds.Rows[0]["BillNo"].ToString();
                        DateTime dt = DateTime.Now;
                        DateTime.TryParse(ds.Rows[0]["BillDate"].ToString(), out dt);
                        Storage.BillDate = dt.ToString("dd/MM/yy") + " " + dt.ToShortTimeString();
                        double.TryParse(ds.Rows[0]["TotalAmount"].ToString(), out PrintCommon.TotalAmt);

                        int companyID;
                        int.TryParse(ds.Rows[0]["CompanyID"].ToString(), out companyID);

                        try
                        {
                            if (PrintCommon.PrintItems != null)
                                PrintCommon.PrintItems.Clear();
                            if (PrintCommon.CreditNoteItems != null)
                                PrintCommon.CreditNoteItems.Clear();
                            if (PrintCommon.BulkOrderItems != null)
                                PrintCommon.BulkOrderItems.Clear();
                            if (PrintCommon.VendorItems != null)
                                PrintCommon.VendorItems.Clear();
                        }
                        catch (Exception) { }

                        ds = Database.GetCreditNoteDetailedBill(Storage.BillNo, companyID.ToString());

                        foreach (DataRow row in ds.Rows)
                        {
                            CreditNoteBillingGridItemControl ctrl = new CreditNoteBillingGridItemControl(companyID);
                            ctrl.txtCode.Text = row["ItemCode"].ToString();
                            ctrl.txtDesc.Text = row["ItemName"].ToString();
                            
                            double price = 0.0d;
                            double qty = 0.0d;
                            double.TryParse(row["SellingPrice"].ToString(), out price);
                            double.TryParse(row["Qty"].ToString(), out qty);

                            ctrl.txtPrice.Text = (price * qty).ToString();
                            ctrl.txtQty.Text = row["Qty"].ToString();
                            PrintCommon.CreditNoteItems.Add(ctrl);
                        }

                        if (ConfigurationManager.AppSettings["PrinterType"] != null && ConfigurationManager.AppSettings["PrinterType"].ToString() == "new")
                        {
                            NewPrinter form = new NewPrinter();
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                        }
                        else
                        {
                            Form1 form = new Form1();
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                        }

                        PrintCommon.CreditNoteItems.Clear();
                        Storage.BillDate = string.Empty;
                    }
                    else
                    {
                        txtAmount.Clear();
                        lblBillResult.Content = "InValid Bill No";
                    }
                }
            }
            catch (Exception) { }
        }
    }
}
